﻿using Apps.BLL.Report;
using Apps.Common;
using Apps.IBLL.LMS;
using Apps.IBLL.PUB;
using Apps.Models;
using Apps.Web.Core;
using Microsoft.Practices.Unity;
using Microsoft.Reporting.WebForms;
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Apps.Web.Areas.Report.Controllers
{
    public class AssayOtherDayReportController : BaseController
    {
        [Dependency]
        public IPUB_SUPPLIERBLL m_SUPPLIERBLL { get; set; }


        //[SupportFilter(ActionName = "Index")]
        //public ActionResult Index(string TYPE_ID, string TRANSPORT_ID, string STime, string ETime)
        //{

        //    ViewBag.TYPE_ID = TYPE_ID;

        //    if (string.IsNullOrWhiteSpace(STime))
        //    {
        //        ViewBag.STime = DateTime.Now.ToString("yyyy-MM-01");  //默认为当天
        //        STime = ViewBag.STime;
        //    }
        //    if (string.IsNullOrWhiteSpace(ETime))
        //    {
        //        ViewBag.ETime = DateTime.Now.ToString("yyyy-MM-dd");
        //        ETime = ViewBag.ETime;
        //    }

        //    ReportViewer reportViewer = new ReportViewer();
        //    ViewBag.ReportViewer = reportViewer;
        //    reportViewer.ShowPrintButton = true;
        //    reportViewer.SizeToReportContent = true;
        //    reportViewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"\ReportFile\AssayReport\AssayDay_Report.rdlc";
        //    reportViewer.LocalReport.SetParameters(new ReportParameter("TabulationCompany", siteConfig.TabulationCompany));
        //    reportViewer.LocalReport.DataSources.Clear();
        //    reportViewer.LocalReport.DataSources.Add(new ReportDataSource { Value = Report_DT(TYPE_ID, STime, ETime), Name = "DataSet1" });
        //    reportViewer.LocalReport.Refresh();

        //    return View();
        //}

        [SupportFilter(ActionName = "Index")]
        public ActionResult Index(string TYPE_ID, string TRANSPORT_ID, string STime, string ETime)
        {
            var data = Report_DT(TYPE_ID, STime, ETime);
            ViewBag.TYPE_ID = TYPE_ID;
            return View(data);
             
        }

        public ActionResult ReportChild(string TYPE_ID, string TRANSPORT_ID, string STime, string ETime)
        {
            if(string.IsNullOrEmpty(STime) &&string.IsNullOrEmpty(ETime))
            {
                STime = DateTime.Now.ToString("yyyy-MM-01");
                ETime = Convert.ToDateTime(DateTime.Now.AddMonths(1).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
            }
            var data = Report_DT(TYPE_ID, STime, ETime);
            ViewBag.TYPE_ID = TYPE_ID;
            return View(data);
        }

        private DataTable Report_DT(string TYPE_ID, string _STime, string _ETime)
        {
            string strConnDB = ConfigurationManager.ConnectionStrings["OracleDbContext"].ToString();
            string _strCmdWhere = " ";
            if (!string.IsNullOrEmpty(TYPE_ID))
                _strCmdWhere += $@"AND AA.TYPE_STR LIKE '%{TYPE_ID}%'";


            if (_STime != null && _STime.Length > 0)
                _strCmdWhere += @" AND AA.UPDATE_TIME >= to_date('" + _STime + "','YYYY-MM-DD')";

            if (_ETime != null && _ETime.Length > 0)
                _strCmdWhere += @" AND AA.UPDATE_TIME <= to_date('" + _ETime + "','YYYY-MM-DD')";

            string _strCmd = string.Format(@"SELECT AA.AUDIT_ID,
                                                   AA.BATCH_TYPE,
                                                   AA.UPDATE_TIME,
                                                   AA.BATCH_CODE,
                                                   AA.TEST_CODE,
                                                   AA.TYPE_STR,
                                                   C.SMALL as STYPE_STR,
                                                   C.MEDIUM as TEST_MODE_STR,
                                                   C.LARGE as TEST_TYPE_STR,
                                                   AA.MT,
                                                   AA.MAD,
                                                   AA.AD,
                                                   AA.VDAF,
                                                   AA.STD,
                                                   AA.G,
                                                   AA.Y,
                                                   AA.X,
                                                   AA.A,
                                                   AA.B,
                                                   AA.QNETAR_KCAL,
                                                   AA.APPROVAL_STATUS,
                                                   AA.TEMPLATE_ID,
                                                   AA.AUDIT_TIME,
                                                   C.SMALL,
												   C.MEDIUM,
												   C.LARGE,
											 	   C.CRI,
												   C.CSR,
												   C.M40,
												   C.M10,
												   C.JTXXLARGE,
												   C.JTXLARGE,
												   C.JTLARGE,
												   C.JTMEDIUM,
												   C.JTSMALL
                                              FROM V_LMS_REPORT AA 
                                              left join V_LMS_RESULT C
                                           ON AA.TEST_CODE = C.TEST_CODE AND C.STYPE = 0
                                           AND C.TEST_TYPE = 0
                                           AND C.TEST_MODE <> 2
                                           AND C.TEST_STATE = 1
                                           WHERE 1=1 AND TYPE_STR IS NOT NULL {0}", _strCmdWhere);
            DataTable _dt = new DataTable();
            Oracle.ManagedDataAccess.Client.OracleDataAdapter _orcDA = new Oracle.ManagedDataAccess.Client.OracleDataAdapter(_strCmd, strConnDB);
            _orcDA.Fill(_dt);
            return _dt;
        }
    }
}